package com.yowasa.xiuxian.service.helper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.entity.po.MysWareHouse;
import com.yowasa.xiuxian.entity.po.WereWolf;
import com.yowasa.xiuxian.entity.po.WolfTicket;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.MysWareHouseMapper;
import com.yowasa.xiuxian.mapper.WereWolfMapper;
import com.yowasa.xiuxian.mapper.WolfTicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class WareHoustHelper {

    @Autowired
    private MysWareHouseMapper wareHouseMapper;

    @Autowired
    private WereWolfMapper wereWolfMapper;

    @Autowired
    private WolfTicketMapper wolfTicketMapper;


    public String checkAdmin(String userId,Integer type) {
        String flag = wareHouseMapper.getFlag(userId,type);
        return flag;
    }

    public MysWareHouse selectItemByUserIdAndFlag(String userId, String flag,String name) {
        MysWareHouse mysWareHouse = wareHouseMapper.selectOne(
                new LambdaQueryWrapper<MysWareHouse>()
                        .eq(MysWareHouse::getFlag, flag)
                        .eq(MysWareHouse::getUserId,userId)
                        .eq(MysWareHouse::getItemName, name));
        return mysWareHouse;
    }

    public void addItem(String flag, String userId, String itemName, Integer num) {
        MysWareHouse myItem = selectItemByUserIdAndFlag(userId, flag, itemName);
        if (myItem != null) {
            myItem.setItemNum(myItem.getItemNum() + num);
            wareHouseMapper.updateById(myItem);
        }else {
            MysWareHouse obj = new MysWareHouse();
            obj.setFlag(flag);
            obj.setItemName(itemName);
            obj.setItemNum(num.longValue());
            obj.setUserId(userId);
            wareHouseMapper.insert(obj);
        }
    }



    public boolean reduceItem(String flag, String userId, String itemName, Integer num) {
        MysWareHouse myItem = selectItemByUserIdAndFlag(userId, flag, itemName);
        if (myItem != null && myItem.getItemNum() >= num) {
            myItem.setItemNum(myItem.getItemNum() - num);
            wareHouseMapper.updateById(myItem);
            return true;
        }
        return false;
    }


    public List<MysWareHouse> getAllByUserId(String userId,String s) {
        LambdaQueryWrapper<MysWareHouse> gt = new LambdaQueryWrapper<MysWareHouse>().eq(MysWareHouse::getUserId, userId).gt(MysWareHouse::getItemNum, 0);
        if(s != null && !s.equals("")){
            gt.eq(MysWareHouse::getFlag,s);
        }
        List<MysWareHouse> mysWareHouses = wareHouseMapper.selectList(gt);
        return mysWareHouses;
    }

    public void insertWolfRoom(String userId, int i,Integer a) {
        WereWolf wereWolf = new WereWolf();
        wereWolf.setRoomId(i);
        wereWolf.setStatus(1);
        wereWolf.setType(0);
        wereWolf.setUserId(userId);
        wereWolf.setUserName(String.valueOf(a));
        wereWolfMapper.insert(wereWolf);
    }

    public WereWolf getWolfRoom(Integer roomId,Integer status) {

        return wereWolfMapper.selectOne(new LambdaQueryWrapper<WereWolf>().eq(WereWolf::getRoomId,roomId).eq(WereWolf::getStatus,status).eq(WereWolf::getType,0));
    }

    public Integer selectWolfCount(Integer roomId) {
        Long aLong = wereWolfMapper.selectCount(new LambdaQueryWrapper<WereWolf>().eq(WereWolf::getRoomId, roomId).eq(WereWolf::getStatus, 1).eq(WereWolf::getType, 1));
        return aLong.intValue();
    }

    public void insertWolfMember(String userId,Integer roomId ,String nickname) {
        WereWolf one = wereWolfMapper.selectOne(new LambdaQueryWrapper<WereWolf>().eq(WereWolf::getRoomId, roomId).eq(WereWolf::getStatus, 1).eq(WereWolf::getType, 1).eq(WereWolf::getUserId, userId));
        if(ObjectUtil.isNotNull(one)){
            throw new HandlerException("你已报名，不可重复报名");
        }
        WereWolf wereWolf = new WereWolf();
        wereWolf.setRoomId(roomId);
        wereWolf.setStatus(1);
        wereWolf.setType(1);
        wereWolf.setUserId(userId);
        wereWolf.setUserName(nickname);
        wereWolfMapper.insert(wereWolf);
    }

    public List<WereWolf> listWolf(Integer roomId) {

        List<WereWolf> wereWolves = wereWolfMapper.selectList(new LambdaQueryWrapper<WereWolf>().eq(WereWolf::getRoomId, roomId).eq(WereWolf::getStatus, 1).eq(WereWolf::getType, 1));

        return wereWolves;
    }

    public void updateWolfById(WereWolf wereWolf) {
        wereWolfMapper.updateById(wereWolf);
    }

    public void insertWolfEmpower(String atUserId,Integer type) {
        wereWolfMapper.insertWolfEmpower(atUserId,type);
    }

    public void deleteWolfEmpower(String atUserId,Integer type) {
        wereWolfMapper.deleteWolfEmpower(atUserId,type);
    }

    public WereWolf getUserWolf(Integer roomId, String userId) {
        return wereWolfMapper.selectOne(new LambdaQueryWrapper<WereWolf>().eq(WereWolf::getRoomId, roomId).eq(WereWolf::getUserId,userId).eq(WereWolf::getStatus, 1).eq(WereWolf::getType, 1));

    }

    public void updateWolf(WereWolf wolf,LambdaQueryWrapper<WereWolf> wrapper) {
        wereWolfMapper.update(wolf, wrapper);
    }

    public List<WereWolf> getLiveWolf(Integer roomId) {
        List<WereWolf> wereWolves = wereWolfMapper.selectList(new LambdaQueryWrapper<WereWolf>()
                .eq(WereWolf::getRoomId, roomId)
                .eq(WereWolf::getStatus, 1)
                .eq(WereWolf::getType, 1)
                .eq(WereWolf::getDead,0));
        return wereWolves;
    }

    public synchronized boolean saveOrUpdateTicket(String uid, Integer number, Integer round, Integer roomId) {

        LambdaQueryWrapper<WolfTicket> wr = new LambdaQueryWrapper<WolfTicket>()
                .eq(WolfTicket::getRoomId, roomId)
                .eq(WolfTicket::getRound, round)
                .eq(WolfTicket::getUserId, "0");
        WolfTicket wolfTicket = wolfTicketMapper.selectOne(wr);
        if(ObjectUtil.isNull(wolfTicket) || wolfTicket.getStatus() == 1){
            return false;
        }
        WolfTicket one = wolfTicketMapper.selectOne(new LambdaQueryWrapper<WolfTicket>()
                .eq(WolfTicket::getRoomId, roomId)
                .eq(WolfTicket::getRound, round)
                .eq(WolfTicket::getUserId, uid));
        if(ObjectUtil.isNotNull(one)){
            return false;
        }
        WolfTicket ticket = new WolfTicket();
        ticket.setChoose(number+"");
        ticket.setRoomId(roomId);
        ticket.setUserId(uid);
        ticket.setRound(round);
        wolfTicketMapper.insert(ticket);
        return true;
    }

    public List<Map<String, String>> listTicket(Integer roomId, int i) {
        LambdaQueryWrapper<WolfTicket> eq = new LambdaQueryWrapper<WolfTicket>().eq(WolfTicket::getRoomId, roomId).eq(WolfTicket::getRound, i);
        WolfTicket wolfTicket = new WolfTicket();
        wolfTicket.setStatus(1);
        wolfTicketMapper.update(wolfTicket,eq);
        List<Map<String, String>> stringStringMap = wereWolfMapper.statsTicket(roomId, i);
        return stringStringMap;
    }

    public List<List<Map<String, String>>> reviewTicket(Integer roomId) {
        List<Integer> list = wereWolfMapper.getReviewRound(roomId);
        List<List<Map<String, String>>> lists = new ArrayList<>();
        list.forEach(e -> {
            List<Map<String, String>> stringStringMap = wereWolfMapper.statsTicket(roomId, e);
            lists.add(stringStringMap);
        });
        return lists;
    }

    public void addTicket(Integer roomId,Integer roundTime) {
        WolfTicket ticket = new WolfTicket();
        ticket.setChoose("");
        ticket.setRoomId(roomId);
        ticket.setUserId("0");
        ticket.setRound(roundTime);
        ticket.setStatus(0);
        wolfTicketMapper.insert(ticket);
    }
}
